<?php

namespace Shared\Main\Repository;
use Doctrine\ORM\EntityRepository;
 
class SiteObject extends EntityRepository
{
    public function getMainPicture(\Shared\Main\SiteObject $Object, $force = false){
        $QB = $this->_em->createQueryBuilder();

        $QB->select('f')
                ->from('Shared\Multimedia\File', 'f')
                ->join('f.SiteObjectGalleryItem', 'sof')
                ->andWhere('sof.SiteObject = :SiteObject');
        
        if ( !$force ) {
            $QB->andWhere('sof.isActive = 1');
        }
        
        $SiteObject = $QB->andWhere('sof.isMain = 1')
                ->setParameter('SiteObject', $Object)
                ->setMaxResults(1)
                ->orderBy('sof.id', 'DESC')
                ->getQuery()->getOneOrNullResult();
        
        return $SiteObject;
    }
    
    public function getAllPictures(\Shared\Main\SiteObject $Object, $force = false){
        $QB = $this->_em->createQueryBuilder();

        $QB->select('sogi')
                ->from('Shared\Multimedia\SiteObjectGalleryItem', 'sogi')
                ->andWhere('sogi.SiteObject = :SiteObject');
        
        if ( !$force ) {
            $QB->andWhere('sogi.isActive = 1');
        }
        
        $pictures = $QB->setParameter('SiteObject', $Object)
                ->orderBy('sogi.id', 'DESC')
                ->getQuery()->getResult();

        return $pictures;
    }
    
    public function getSOGI(\Shared\Main\SiteObject $Object, $force = false){
        $QB = $this->_em->createQueryBuilder();

        $QB->select('sogi')
                ->from('Shared\Multimedia\SiteObjectGalleryItem', 'sogi')
                ->andWhere('sogi.SiteObject = :SiteObject');
        
        if ( !$force ) {
            $QB->andWhere('sogi.isActive = 1');
        }
        
        $SiteObject = $QB->andWhere('sogi.isMain = 1')
                ->setParameter('SiteObject', $Object)
                ->setMaxResults(1)
                ->orderBy('sogi.id', 'DESC')
                ->getQuery()->getOneOrNullResult();
        
        return $SiteObject;
    }
}
